<extend name="Public/base"/>

<block name="body">
    <div class="main-title">
        <h2>{$_title}</h2>
    </div>
    <notempty name="task" >
    	<form action="{:U('Task/editTask')}" method="post" class="form-horizontal">
    		<input type="hidden" name="id" value="{$task.id}" />
    <else />
	    <form action="{:U('Task/addTask')}" method="post" class="form-horizontal">
    </notempty>
        <div class="form-item">
            <label class="item-label">任务名称<span class="check-tips"></span></label>
            <div class="controls">
                <input type="text" class="text input-large" name="name" value="{$task.name|default=''}">
            </div>
        </div>
        <div class="form-item">
            <label class="item-label">任务标题<span class="check-tips"></span></label>
            <div class="controls">
                <input type="text" class="text input-large" name="title" value="{$task.title|default=''}">
            </div>
        </div>
		<div class="form-item">
			<label class="item-label">任务商品id<span class="check-tips"></span></label>
			<div class="controls">
				<input type="text" class="text input-large" name="target_url" value="{$task.target_url|default=''}">
			</div>
		</div>
         <div class="form-item">
            <label class="item-label">任务类型<span class="check-tips"></span></label>
            <div class="controls">
            	<select name="type_id"  id="type_id" class="input-large">
            		<notempty name="taskType">
            			<foreach name="taskType" key="key" item="val">
            				<if condition="$val['id'] eq $task['type_id']">
		            			<option value="{$val.id}" selected>{$val.name}</option>
	            			<else />
	            				<option value="{$val.id}">{$val.name}</option>
            				</if>
            			</foreach>
            		</notempty>
            	</select>
            </div>
        </div>
        
        <div class="form-item">
            <label class="item-label">关联文件类型<span class="check-tips"></span></label>
            <div class="controls">
            	<select id="app_file_type"  class="input-large">
            		<option value="0">***请选择关联文件类型***</option>
            		<foreach name="fileTypeList" key="key" item="val">
            			<if condition="$fileType eq $key">
            				<option value="{$key}" selected>{$val}</option>
            			<else />
	            			<option value="{$key}">{$val}</option>
            			</if>
            		</foreach>
            	</select>
            </div>
        </div>
        
        <div class="form-item">
            <label class="item-label">文件列表<span class="check-tips"></span></label>
            <div class="controls">
                <div class="div-desc" id="file_list"></div>
            </div>
        </div>
        
        <div class="form-item" style="display:none;" id="task_img_1">
            <label class="item-label">任务图片<span class="check-tips"></span></label>
            <div class="controls div-desc" id="task_img_2"></div>
        </div>
        
        <div class="form-item">
            <label class="item-label">任务奖励<span class="check-tips">（大于等于1表示金额，小于1表示收益分成）</span></label>
            <div class="controls">
                <input type="text" class="text input-large" name="money" value="{$task.money|default='0'}">
            </div>
        </div>
        
        <div class="form-item">
            <label class="item-label">开始时间<span class="check-tips">格式为YYYY-MM-DD HH</span></label>
            <div class="controls">
                <notempty name="task">
                	<input type="text" class="text input-large date_picker" name="start_time" value="{:date('Y-m-d H:i:s', $task['start_time'])}">
                <else />
                	<input type="text" class="text input-large date_picker" name="start_time" value="">
                </notempty>
            </div>
        </div>
        
        <div class="form-item">
            <label class="item-label">结束时间<span class="check-tips">（格式为YYYY-MM-DD HH，不输入表示永远有效）</span></label>
            <div class="controls">
            	<if condition="$task['end_time'] eq 0">
            		<input type="text" class="text input-large date_picker" name="end_time" value="">
            	<else />
	                <input type="text" class="text input-large date_picker" name="end_time" value="{:date('Y-m-d H:i:s', $task['end_time'])}">
            	</if>
            </div>
        </div>
        
        <div class="form-item">
            <label class="item-label">任务描述<span class="check-tips"></span></label>
            <div class="controls">
                <textarea name="description" class="kind-editor" cols="50" rows="5">{$task.description|default=''}</textarea>
            </div>
        </div>
        
        <div class="form-item">
            <button class="btn submit-btn ajax-post" id="submit" type="submit" target-form="form-horizontal">确 定</button>
            <button class="btn btn-return" onclick="javascript:history.back(-1);return false;">返 回</button>
        </div>
    </form>
</block>
<block name="style">
	<link href="__PUBLIC__/static/jquery-datetimepicker/jquery-datetimepicker.css" rel="stylesheet" media="screen">
	<link rel="stylesheet" href="__PUBLIC__/static/kindeditor/default/default.css" />
</block>
<block name="script">
	<script charset="utf-8" src="__PUBLIC__/static/kindeditor/kindeditor-min.js"></script>
	<script charset="utf-8" src="__PUBLIC__/static/kindeditor/zh_CN.js"></script>
	<script type="text/javascript" src="__PUBLIC__/static/jquery-datetimepicker/jquery-datetimepicker.js"></script>
    <script type="text/javascript">
    
    KindEditor.ready(function(K) {
		window.editor = K.create('textarea[class="kind-editor"]', {
			//allowFileManager : false
			minWidth:900,
			minHeight:300,
			uploadJson:"{:U('File/kinderUpload')}",
			afterBlur: function(){this.sync();},
		});
	});
    
  //日期时间插件
	$('.date_picker').datetimepicker({
		lang:'ch',
		format:'Y-m-d H:00',
		timepicker:true,
		allowBlank:true
	});
	fileids = {$fileids};
  //文件列表
  $("#app_file_type").change(function(){
	  var val = $(this).val();
	  var fileList = $("#file_list");
	  if(val == 0) {
		  fileList.empty().append('请选择文件类型');
		  return;
	  }
	  
	  //查询文件列表
	 ajaxRequest("{:U('File/fileList1')}", {'file_type':val}, {'func':function(data){
		//处理查询结果
			info = '';
			if(data && data.error == 0) {
				$.each(data.info, function(key, val){
					//文件列表
					info += '<span style="cursor:pointer">';
					/* <notempty name="task">
						var ids = "{$task['file_ids']}";
						ids = ids.split(',');
						$.each(ids, function(index, v){
							if(val == v) {
								info += '<input type="checkbox" selected style="vertical-align:middle" value="'+val.id+'" name="file_ids[]"" /> ';
								return false;
							}
						});
					<else/>
						info += '<input type="checkbox" style="vertical-align:middle" value="'+val.id+'" name="file_ids[]"" /> ';
					</notempty> */
					val.checkeds = "";
					$.each(fileids,function(i,v){
						if(val.id ==v ){
							val.checkeds = "checked";
						}
					})
					info += '<input type="checkbox" style="vertical-align:middle" value="'+val.id+'" name="file_ids[]" '+val.checkeds+'/> ';
					info +='<span class="avatar_file">' + val.name + '.' + val.ext + '</span></span>';
					info += '<br/>';
				});
				
				fileList.empty().append(info);
				
				//点击选择
				$('.avatar_file').click(function(){
					$this = $(this).siblings('input');
					var val = $this.prop('checked');
					if(val) {
						$this.removeAttr('checked');
					} else {
						$this.prop('checked', 'true');
					}
				});
			} else {
				fileList.empty().append('没有文件');
			}
	 }}); 
  });
  
  $("#app_file_type").change();
  //任务文件
/*   $("#app_file_type").change(function(){
	  var val = $(this).val();
	  
	  //如果是编辑，则获取已存在的文件id
	  var taskId = false;
	  <notempty name="task">
			taskId = "{$task.id}";
	  </notempty>
	  
	  //查询任务文件信息
	 ajaxRequest("{:U('Task/taskFile')}", {'task_type_id':val,'task_id':taskId}, {'func':function(data){
		 if(data && data.error == 0) {
			 $("#task_img_1").css('display', 'block');
			 var info = '<div>';
			 var i = 0;
			 $.each(data.msg, function(index, item){
				info += '<span style="margin:5px 10px 5px 5px;">';
				
				if(item.selected) {
					info += '<input type="checkbox" name="file_ids[]" checked value="'+item.id+'"/>';
				}  else {
					info += '<input type="checkbox" name="file_ids[]" value="'+item.id+'"/>';
				}
				
				info += '<a href="http://www.fund.com:8081' +item.url+'">' + item.name + '</a></span>'; 
				i++;
				if( i % 5 == 0) {
					info += '<br/>';
				}
			 });
			 info += '</div>';
			// info += '<input type="hidden" name="fileIds" value=""/>';
			 $("#task_img_2").empty().append(info);
		 } else {
			 $("#task_img_1").css('display', 'none');
			 var msg = data ? data.msg : '文件信息获取失败';
			 alertDialog(msg, 'error');
		 }
	 }}); 
  }); */
  $("#type_id").change();
	//导航高亮
	highlight_subnav('{:U('Task/taskList')}');
</script>
</block>
